//
//  MainViewController.m
//  
//
//  Created by qkong on 15/6/15.
//
//

#import "MainViewController.h"
#import "QAirAppDelegate.h"
#import "QAir.h"
#import <CoreBluetooth/CoreBluetooth.h>
#import "Tool.h"
#import "UIView+Toast.h"
#import "HistoryChartViewController.h"
typedef enum{
    BtnAirTag = 1000,
    BtnHchoTag,
    BtnPm25Tag,
    BtnHumidityTag,
    BtnTemperatureTag,
} ListBtnTag;

@interface MainViewController ()

@property (weak, nonatomic) IBOutlet UILabel *airLabel;

@property (weak, nonatomic) IBOutlet UILabel *hchoLabel;

@property (weak, nonatomic) IBOutlet UILabel *pm25Label;

@property (weak, nonatomic) IBOutlet UILabel *humidityLabel;

@property (weak, nonatomic) IBOutlet UILabel *temperatureLabel;

@property (weak, nonatomic) IBOutlet UILabel *noiseLabel;

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter]addObserver:self
                                        selector:@selector(updateQAirData:)
                                            name:UPDATE_QAIR_NOTIFICATION_NAME
                                          object:nil];
}

- (void)updateQAirData:(NSNotification *)notification{
    QAir *qair = [[notification userInfo] objectForKey:@"QAir"];
    self.airLabel.text = qair.air;
    self.hchoLabel.text  = [NSString stringWithFormat:@"%@mg",qair.hcho];
    self.pm25Label.text = [NSString stringWithFormat:@"%@ug",qair.pm25];
    self.humidityLabel.text = [NSString stringWithFormat:@"%@%%",qair.humidity];
    self.temperatureLabel.text = [NSString stringWithFormat:@"%@℃",qair.temperature];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter]removeObserver:self
                                                   name:UPDATE_QAIR_NOTIFICATION_NAME
                                                 object:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)showSideBar:(id)sender {
    QAirAppDelegate *delegate = (QAirAppDelegate *)[UIApplication sharedApplication].delegate;
    [delegate.container toggleLeftSideMenuCompletion:nil];
}


- (IBAction)getHistory:(UIButton *)sender {
    QAirAppDelegate *delegate = (QAirAppDelegate *)[UIApplication sharedApplication].delegate;
    delegate.historyWriteData = [self getPeripheralWriteDataWithTag:sender.tag];
    CBPeripheral *qairPeripheral = delegate.qairPeripheral;
    if (qairPeripheral.state != CBPeripheralStateConnected) {
        [self.view makeToast:@"蓝牙已经断开" duration:2.0 position:CSToastPositionBottom];
        return;
    }
    for (CBService *service in qairPeripheral.services) {
        if ([SERVICES_UUID containsString:[service.UUID.UUIDString lowercaseString]]) {
            CBUUID *get_history_characteristci_uuid = [CBUUID UUIDWithString:GET_HISTORY_CHARACTERISTIC_UUID];
            [qairPeripheral discoverCharacteristics:@[get_history_characteristci_uuid]
                                     forService:service];
        }
    }
    HistoryChartViewController *hCharVC = [[HistoryChartViewController alloc]initWithNibName:@"HistoryChartViewController" bundle:nil];
    [self presentViewController:hCharVC animated:YES completion:^{
        hCharVC.historyTitle.text = [self getTitleWithTag:sender.tag];
    }];
}

- (NSData *)getPeripheralWriteDataWithTag:(ListBtnTag)tag{
    NSData *peripheralWriteData = nil;
    switch (tag) {
        //污染指数
        case BtnAirTag:
        {
            peripheralWriteData = [Tool dataWithHexString:@"05"];
        }
            break;
        //甲醛
        case BtnHchoTag:
        {
            peripheralWriteData = [Tool dataWithHexString:@"04"];
        }
            break;
        //pm2.5
        case BtnPm25Tag:
        {
            peripheralWriteData = [Tool dataWithHexString:@"03"];
        }
            break;
        //湿度
        case BtnHumidityTag:
        {
            peripheralWriteData = [Tool dataWithHexString:@"02"];
        }
            break;
        //温度
        case BtnTemperatureTag:
        {
            peripheralWriteData = [Tool dataWithHexString:@"01"];
        }
            break;
    }
    
    return peripheralWriteData;
}

- (NSString *)getTitleWithTag:(ListBtnTag)tag{
    switch (tag) {
            //污染指数
        case BtnAirTag:
        {
            return @"污染指数";
        }
            break;
            //甲醛
        case BtnHchoTag:
        {
            return @"甲醛";
        }
            break;
            //pm2.5
        case BtnPm25Tag:
        {
            return @"pm2.5";
        }
            break;
            //湿度
        case BtnHumidityTag:
        {
            return @"湿度";
        }
            break;
            //温度
        case BtnTemperatureTag:
        {
            return @"温度";
        }
            break;
    }
}

@end
